﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Data;

namespace WeiboPlus.Shell.Converter
{
    public class TimeToStringConverter :IValueConverter
    {

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            var format = parameter as string;
            if (string.IsNullOrEmpty(format))
            {
                return ((DateTime)value).ToShortDateString();
            }
            else if (format == "FROM_NOW")
            {
                return GetFromNow((DateTime)value);
            }
            else
            {
                return ((DateTime)value).ToString(format);
            }

            throw new NotImplementedException();
        }

        public static string GetFromNow(DateTime dateTime)
        {
            var now = DateTime.Now;

            var timespan = DateTime.Now.Subtract(dateTime);

            if (timespan.TotalMinutes < 2)
            {
                return "刚刚";
            }
            else if (timespan.TotalMinutes < 60)
            {
                return timespan.Minutes.ToString() + "分钟前";
            }
            else if (timespan.TotalMinutes < 120)
            {
                return "1小时前";
            }
            else if (timespan.TotalHours < 24)
            {
                return timespan.Hours.ToString() + "小时前";
            }
            else if (timespan.TotalDays < 30)
            {
                return timespan.Days.ToString() + "天前";
            }
            else if (Math.Round(timespan.TotalDays / 30) < 24)
            {
                return Math.Round(timespan.TotalDays / 30).ToString() + "个月前";
            }
            else
            {
                return DateTime.Now.Year - dateTime.Year + "年前";
            }
            //throw new NotImplementedException();
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}
